package com.example.administrator.myapplication;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * Created by Administrator on 2019/3/18.
 */

public class MyActivity extends AppCompatActivity {
    Button mBtnUserlogin;
    EditText mEtUsername;
    EditText mEtPassword;
CheckBox mCkUsername,mCkPassword;
    @Override

    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);
        mEtPassword = findViewById(R.id.et_password);
        mBtnUserlogin = findViewById(R.id.bt_user_login);
        mEtUsername = findViewById(R.id.et_username);
        mCkUsername=findViewById(R.id.ck_rmusername);
        mCkPassword=findViewById(R.id.ck_rmpassword);

        mBtnUserlogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = mEtUsername.getText().toString();
                String password = mEtPassword.getText().toString();
//                String s1= "admin";
//                String s2="123";
                //根据意图跳转activity




                if (username.equals("admin") && password.equals("123")){
                    //神奇的跳转意图
                    Bundle bundle = new Bundle();
                    bundle.putString("username", username);
                    bundle.putString("password", password);
                    Intent intent = new Intent(MyActivity.this, MainActivity.class);
                    intent.putExtras(bundle);
                    startActivity(intent);

                    //在intent中携带数据
                    if(mCkUsername.isChecked()&&mCkPassword.isChecked()) {
                        try {
                            FileOutputStream fileOutputStream = openFileOutput("1.txt", MODE_PRIVATE);
                   /* byte[] msg=username.getBytes("utf-8");
                    byte[] msg1=password.getBytes("utf-8");
                    fileOutputStream.write(msg);
                    fileOutputStream.write(msg1);*/
                            fileOutputStream.write((username + ":" + password).getBytes());
                            fileOutputStream.close();
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }else if( mCkPassword.isChecked()){
                        try {
                            FileOutputStream fileOutputStream = openFileOutput("1.txt",  MODE_PRIVATE);
                            fileOutputStream.write((password).getBytes());
                            fileOutputStream.close();
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                    }else {
                        try {
                            FileOutputStream fileOutputStream = openFileOutput("1.txt",  MODE_PRIVATE);
                            fileOutputStream.write((username).getBytes());
                            fileOutputStream.close();
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }


                    }
                } else{

                    Toast.makeText(MyActivity.this, "账号或密码错误！", Toast.LENGTH_LONG).show();
                }


            }
        });
    }
}



